<html>
<!-- =====================================================================

  File:      OrdersDetail.htm for Adventure Works Cycles Storefront Sample
  Summary:   Self-documentation for application
  Date:	     June 16, 2003

=====================================================================

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= -->
    <head>
        <title>Adventure Works Cycles Store Documentation</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body class="NormalIndent">
        <h1>"OrdersDetail" Stored Procedure</h1>

        <b>Description:</b>

        <P>This stored procedure accepts an OrderID as input and returns a recordset containing line items associated with the order
        and 4 output parameters containing details of the given OrderID. </P>

        <b>Definition:</b>

<pre style="background-color:white">
<span style="color: #0000FF"><strong>CREATE</strong></span> Procedure usp_OrdersDetail
<span style="color: #4444FF">(</span>
    @OrderID    <span style="color: #2040a0"><strong>int</strong></span><span style="color: #4444FF">,</span>
	@Culture <span style="color: #2040a0"><strong>nvarchar</strong></span><span style="color: #4444FF">(</span>10<span style="color: #4444FF">)</span><span style="color: #4444FF">,</span>
    @OrderDate  <span style="color: #2040a0"><strong>datetime</strong></span> OUTPUT<span style="color: #4444FF">,</span>
    @ShipDate   <span style="color: #2040a0"><strong>datetime</strong></span> OUTPUT<span style="color: #4444FF">,</span>
    @OrderTotal <span style="color: #2040a0"><strong>nvarchar</strong></span><span style="color: #4444FF">(</span>20<span style="color: #4444FF">)</span> OUTPUT
<span style="color: #4444FF">)</span>
<span style="color: #0000FF"><strong>AS</strong></span>

<span style="color: #008000">-- Return the order dates from the Orders</span>
<span style="color: #008000">--   Also verifies the order exists for this customer. </span>


<span style="color: #0000FF"><strong>SELECT</strong></span> 
    @OrderDate <span style="color: #4444FF">=</span> OrderDate<span style="color: #4444FF">,</span>
    @ShipDate <span style="color: #4444FF">=</span> ShipDate
    
<span style="color: #0000FF"><strong>FROM</strong></span>    
    Sales.SalesOrderHeader
    
<span style="color: #0000FF"><strong>WHERE</strong></span>   
    SalesOrderID <span style="color: #4444FF">=</span> @OrderID;


IF @@Rowcount <span style="color: #4444FF">=</span> 1
<span style="color: #0000FF"><strong>BEGIN</strong></span>


<span style="color: #008000">/* First, return the OrderTotal out param */</span>
<span style="color: #0000FF"><strong>SELECT</strong></span>  
    @OrderTotal <span style="color: #4444FF">=</span> dbo.ConvertCurrency<span style="color: #4444FF">(</span>SUM<span style="color: #4444FF">(</span>SOD.OrderQty <span style="color: #4444FF">*</span> SOD.UnitPrice<span style="color: #4444FF">)</span><span style="color: #4444FF">,</span> @Culture<span style="color: #4444FF">)</span>.ToString<span style="color: #4444FF">(</span><span style="color: #4444FF">)</span>
    
<span style="color: #0000FF"><strong>FROM</strong></span>    
    Sales.SalesOrderDetail <span style="color: #0000FF"><strong>AS</strong></span> SOD
    
<span style="color: #0000FF"><strong>WHERE</strong></span>   
    SalesOrderID<span style="color: #4444FF">=</span> @OrderID;

<span style="color: #008000">/* Then, return the recordset of info */</span>
<span style="color: #0000FF"><strong>SELECT</strong></span>  
    P.ProductID<span style="color: #4444FF">,</span> 
    P.[<span style="color: #0000FF"><strong>Name</strong></span>]<span style="color: #4444FF">,</span>
    P.ProductNumber<span style="color: #4444FF">,</span>
    dbo.ConvertCurrency<span style="color: #4444FF">(</span>SOD.UnitPrice<span style="color: #4444FF">,</span> @Culture<span style="color: #4444FF">)</span>.ToString<span style="color: #4444FF">(</span><span style="color: #4444FF">)</span> <span style="color: #0000FF"><strong>as</strong></span> UnitPrice<span style="color: #4444FF">,</span>
    SOD.OrderQty<span style="color: #4444FF">,</span>
    dbo.ConvertCurrency<span style="color: #4444FF">(</span>SOD.OrderQty <span style="color: #4444FF">*</span> SOD.UnitPrice<span style="color: #4444FF">,</span> @Culture<span style="color: #4444FF">)</span>.ToString<span style="color: #4444FF">(</span><span style="color: #4444FF">)</span> <span style="color: #0000FF"><strong>as</strong></span> ExtendedAmount

<span style="color: #0000FF"><strong>FROM</strong></span>
    Sales.SalesOrderDetail <span style="color: #0000FF"><strong>AS</strong></span> SOD
  INNER JOIN Production.Product <span style="color: #0000FF"><strong>as</strong></span> P <span style="color: #0000FF"><strong>ON</strong></span> SOD.ProductID <span style="color: #4444FF">=</span> P.ProductID
  
<span style="color: #0000FF"><strong>WHERE</strong></span>   
    SalesOrderID <span style="color: #4444FF">=</span> @OrderID
<span style="color: #0000FF"><strong>ORDER BY</strong></span>
      SOD.SalesOrderDetailID;

<span style="color: #0000FF"><strong>END</strong></span>


</pre>

        <b>Database Tables Used: </b>

        <p><i>SalesOrderHeader:</i>
        &nbsp;The SalesOrderHeader table contains the header information about all the orders in
        the system (i.e., all sales transactions that are actually completed. Users must check out
        their cart to place an order in the system). When an order is created, an entry
        is made in the SalesOrderHeader table with the CustomerID, OrderDate and ShipDate. Then,
        any information about the products in the order is added to the SaleOrderDetail
        table as discussed below. Note that if no OrderDate or ShipDate is provided,
        the current date is entered as default.
        <p>
            <i>SalesOrderDetail:</i>
        &nbsp;The SalesOrderDetail table contains detailed product information for each
        order in the system. For each product in an order, an entry is made into the
        SalesOrderDetail table containing the ProductID, Quantity and current UnitCost of
        the item. There is a many to one relationship between SalesOrderDetail table and
        the SalesOrderHeader table. Note the primary key in the table is both SalesOrderID and
        LineNumber (since those two columns are the minimum required to guarantee
        uniqueness of a record).
        <p>
            <IMG src="1x1.gif" width="25"> <IMG src="../docs/images/SalesOrderHeader-Detail.gif" width="585" height="455">
 
        <P><i>Product:</i> &nbsp;The Product table contains the core information about all of the items for sale
        on the Adventure Works Cycles web site. Its primary key is the ProductID identity field. 

        <p>

        <img src="1x1.gif" width=25> <IMG src="../docs/images/product.gif" width="386" height="501">

    </body>
</html>
